﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class frm_Them_TG : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public frm_Them_TG()
        {
            InitializeComponent();
        }

        private void dr_ds_TG_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_MaTG.Text = dr_ds_TG.CurrentRow.Cells[0].Value.ToString();
            txt_TenTG.Text = dr_ds_TG.CurrentRow.Cells[1].Value.ToString();
            temp = dr_ds_TG.CurrentRow.Cells[0].Value.ToString();
            txt_MaTG.Enabled = false;
            txt_TenTG.Enabled = false;
        }

        private void frm_Them_TG_Load(object sender, EventArgs e)
        {
            Load_Data_TG();
            
        }
        public void Load_Data_TG()
        {
            dr_ds_TG.DataSource = sv.getTG();
            btn_LuuTG.Enabled = false;
            btn_HuyTG.Enabled = false;
        }

        private void btn_ThemTG_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_ThemTG.Enabled = true;
            btn_LuuTG.Enabled = true;
            btn_HuyTG.Enabled = true;
            btn_SuaTG.Enabled = false;
            btn_XoaTG.Enabled = false;
            btn_ThoatTG.Enabled = true;
            txt_MaTG.Enabled = true;
            txt_TenTG.Enabled = true;
            txt_MaTG.Text = "";
            txt_TenTG.Text = "";
            txt_MaTG.Focus();
            
        }

        private void btn_LuuTG_Click(object sender, EventArgs e)
        {
            string a = txt_MaTG.Text;
            string b = txt_TenTG.Text;
            ServiceReference1.TON_GIAO tg = new ServiceReference1.TON_GIAO();
            tg.Ma_TG = Convert.ToInt32(a);
            tg.TenTG = b;

            if (a == "" | b == "")
            {
                MessageBox.Show("Nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.check_tg(tg))
                        kq = -1;
                    else
                        kq = sv.addTG(tg);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setTG(tg);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_Data_TG();
                    btn_SuaTG.Enabled = true;
                    btn_XoaTG.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lôi!!!");
                }
            }
        }

        private void btn_HuyTG_Click(object sender, EventArgs e)
        {
            btn_HuyTG.Enabled = false;
            btn_LuuTG.Enabled = false;
            btn_SuaTG.Enabled = true;
            btn_XoaTG.Enabled = true;
        }

        private void btn_XoaTG_Click(object sender, EventArgs e)
        {
            ServiceReference1.TON_GIAO tg = new ServiceReference1.TON_GIAO();
            tg.Ma_TG = Convert.ToInt32(temp);
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteTG(tg);
                Load_Data_TG();
                txt_MaTG.Text = "";
                txt_TenTG.Text = "";
            }
        }

        private void btn_SuaTG_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_HuyTG.Enabled = true;
            btn_LuuTG.Enabled = true;
            txt_TenTG.Enabled = true;
            txt_TenTG.Enabled = true;
        }

        private void btn_ThoatTG_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát khỏi bảng này?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}
